package cn.jhc.Adapter;

import java.util.ArrayList;

import com.example.shilehui.HelpTopicDetailActivity;
import com.example.shilehui.R;
import com.example.shilehui.UserMeritsBookActivity;

import cn.jhc.Adapter.CellAdapter.DataWrapper;
import cn.jhc.bean.MeritsBook;
import cn.jhc.service.ImageDown;
import cn.jhc.service.OnImageDownload;
import cn.jhc.utils.URL;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MeritsBookAdapter extends BaseAdapter {
	ListView ranking_list;
	ImageDown mImageDown;
	Activity mainActivity;
	private ArrayList<MeritsBook> ranking_data;
	private Context context;
	private LayoutInflater inflater;
	private DataWrapper dataWrapper;
	public MeritsBookAdapter(Context context,ArrayList<MeritsBook>  ranking_data ,Activity mainActivity,ListView ranking_list) {
		this.mainActivity=mainActivity;
		this.ranking_list=ranking_list;
		this.context=context;
		this.ranking_data=ranking_data;
		inflater = (LayoutInflater) this.context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return ranking_data.size();
	}

	@Override
	public Object getItem(int position) {
		return ranking_data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView==null){
			convertView = inflater.inflate(R.layout.tab_host_meritsbook_list, null);
			ImageView imageView=(ImageView) convertView.findViewById(R.id.randing_listview_image);
			TextView textView_name=(TextView) convertView.findViewById(R.id.randing_listview_name);
			TextView textView_index=(TextView) convertView.findViewById(R.id.randing_listview_index);
			TextView textView_signature=(TextView) convertView.findViewById(R.id.randing_listview_signature);
			dataWrapper=new DataWrapper(imageView, textView_name, textView_index, textView_signature);
			convertView.setTag(dataWrapper);
		}else{
			dataWrapper = (DataWrapper) convertView.getTag();
		}
		dataWrapper.textView_name.setText(ranking_data.get(position).getNickname());
		dataWrapper.textView_index.setText(String.valueOf(ranking_data.get(position).getHits()));
		dataWrapper.textView_signature.setText(ranking_data.get(position).getSingleLanage());
		//dataWrapper.textView_name.setText(ranking_data.get(position).getNickname());
		
		if(mImageDown==null){
			mImageDown=new ImageDown();
		}
		
		String url=ranking_data.get(position).getHeadImage();
		
		if(url.startsWith("http")){
			
		}
		else
			url="http://www.shilehui.com"+url;
		dataWrapper.imageView.setTag(url);	
		dataWrapper.imageView.setImageBitmap(null);
		convertView.setOnClickListener(new ImageViewLister(ranking_data.get(position).getUserID(),ranking_data.get(position).getNickname()));
		mImageDown.imageDownload1(url, dataWrapper.imageView, URL.imageLoadingFileURL,
				mainActivity, new OnImageDownload() {
					@Override
					public void onDownloadSucc(Bitmap bitmap, String c_url,
							ImageView mimageView) {
						ImageView imageView = (ImageView) ranking_list
								.findViewWithTag(c_url);
						
						if (imageView != null) {
							imageView.setImageBitmap(bitmap);
							imageView.setTag("");
						}
					}
				});
		
		return convertView;
	}
	public final class DataWrapper {
	
		public ImageView imageView;
		public TextView textView_name;
		public TextView textView_index;
		public TextView textView_signature;
		
		public DataWrapper(ImageView imageView, TextView textView_name,
				TextView textView_index, TextView textView_signature) {
			this.imageView = imageView;
			this.textView_name = textView_name;
			this.textView_index = textView_index;
			this.textView_signature = textView_signature;
		}
		
		

	}

	private class ImageViewLister implements OnClickListener {

		private String ID="id";
		private String id;
		private String Nickname;

		public ImageViewLister(long id,String Nickname) {
			this.Nickname=Nickname;
			this.id = String.valueOf(id);
		}

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(context, UserMeritsBookActivity.class);
			Bundle bundle=new Bundle();
			bundle.putCharSequence(ID, id);
			bundle.putCharSequence("Nickname", Nickname);
			intent.putExtras(bundle);
			context.startActivity(intent);
		}

	}
}
